<template>
  <MainLayout>
    <div class="category-list">
      <h1 class="page-title">分类列表</h1>
      <div class="category-tree" v-if="categories.length">
        <div v-for="category in categories" :key="category.id" class="category-item">
          <router-link :to="`/categories/${category.slug}`" class="category-link">
            <h2 class="category-title">{{ category.name }}</h2>
            <p class="category-description">{{ category.description }}</p>
            <div class="category-meta">
              <span>{{ category.document_count }} 篇文档</span>
            </div>
          </router-link>
          <div v-if="category.children && category.children.length" class="subcategories">
            <div v-for="child in category.children" :key="child.id" class="category-item">
              <router-link :to="`/categories/${child.slug}`" class="category-link">
                <h3 class="category-title">{{ child.name }}</h3>
                <p class="category-description">{{ child.description }}</p>
                <div class="category-meta">
                  <span>{{ child.document_count }} 篇文档</span>
                </div>
              </router-link>
            </div>
          </div>
        </div>
      </div>
      <div v-else class="empty-state">暂无分类</div>
    </div>
  </MainLayout>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import MainLayout from '../components/layout/MainLayout.vue'
import { getCategoryTree } from '../api/category'

const categories = ref([])

// 获取分类树
const fetchCategories = async () => {
  try {
    const res = await getCategoryTree()
    categories.value = res.data
  } catch (error) {
    console.error('获取分类列表失败:', error)
  }
}

onMounted(() => {
  fetchCategories()
})
</script>

<style scoped>
.category-list {
  padding: 20px 0;
}

.page-title {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 40px;
  color: #333;
}

.category-tree {
  display: grid;
  gap: 30px;
}

.category-item {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.category-link {
  display: block;
  padding: 20px;
  text-decoration: none;
  color: inherit;
  transition: background-color 0.3s;
}

.category-link:hover {
  background-color: #f5f5f5;
}

.category-title {
  font-size: 20px;
  font-weight: bold;
  margin: 0 0 10px;
  color: #333;
}

.category-description {
  font-size: 14px;
  color: #666;
  margin: 0 0 10px;
  line-height: 1.5;
}

.category-meta {
  font-size: 12px;
  color: #999;
}

.subcategories {
  margin-left: 40px;
  margin-bottom: 20px;
  display: grid;
  gap: 20px;
}

.subcategories .category-item {
  box-shadow: none;
  border: 1px solid #eee;
}

.empty-state {
  text-align: center;
  padding: 40px;
  color: #999;
  font-size: 14px;
  background-color: #f5f5f5;
  border-radius: 8px;
}
</style> 